package cn.tedu.lucky.draw.server.controller;

import cn.tedu.lucky.draw.server.common.annotation.OperationLog;
import cn.tedu.lucky.draw.server.common.handler.DrawException;
import cn.tedu.lucky.draw.server.common.result.JsonResult;
import cn.tedu.lucky.draw.server.protocol.po.Activity;
import cn.tedu.lucky.draw.server.protocol.po.Prize;
import cn.tedu.lucky.draw.server.protocol.query.ActivityQuery;
import cn.tedu.lucky.draw.server.service.IActivityService;
import com.fasterxml.jackson.annotation.JsonAlias;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.json.JSONException;
import org.springframework.batch.core.*;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin // 允许跨域请求
@Api(tags = "活动管理")
public class ActivityController {

    @Autowired
    private IActivityService activityService;

    @Autowired
    private JobLauncher jobLauncher;

    @Autowired
    private Job csvToDBJob;

    @Autowired
    private JobExplorer jobExplorer;

    @PostMapping("/draw/activity/getAll")
    public JsonResult<List<Activity>> getAll(@RequestBody ActivityQuery activityQuery){
        List<Activity> activities = activityService.getAll(activityQuery);
        return JsonResult.ok(activities);
    }

    @PostMapping("/draw/activity/save")
    @OperationLog(operation = "新增活动")
    public JsonResult save(@RequestBody Activity activity){
        activityService.save(activity);
        return JsonResult.ok();
    }

    @GetMapping("/activity/csvToDB")
    @OperationLog(operation = "批量导入活动")
    public String csvToDB() throws JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobParametersInvalidException, JobRestartException {
        /**
         * new JobParameters() 创建一个空的JobParameters对象，用于启动Job
         * jobExplorer 用于获取Job的运行状态，管理作业元数据和执行历史等信息
         * getNextJobParameters() 这个方法调用会基于之前作业的执行情况来生成新得JobParameters，用于启动新的作业实例
         */
        JobParameters jobParameters = new JobParametersBuilder(new JobParameters(), jobExplorer).getNextJobParameters(csvToDBJob).toJobParameters();
        JobExecution run = jobLauncher.run(csvToDBJob, jobParameters);
        return run.getId().toString();
    }

    @PostMapping("/draw/activity/{id}")
    public JsonResult<Activity> getById(@PathVariable("id") Long id){
        Activity activity = activityService.findById(id);
        return JsonResult.ok(activity);
    }

    @GetMapping("/app/draw/activity/getAll")
    @ApiOperation(value = "获取App端所有活动信息", notes = "获取App端所有活动信息")
    public JsonResult<List<Activity>> getAllActivities() {
        return JsonResult.ok(activityService.getAllActivities());
    }

    @GetMapping("/app/draw/activity/trigger")
    @ApiOperation(value = "触发抽奖", notes = "触发抽奖")
    public JsonResult<Prize> trigger(Long activityId) throws JSONException {
        Prize prize = activityService.trigger(activityId);
        return JsonResult.ok(prize);
    }


}